package com.Ruier.controller;

import com.Ruier.pojo.Hero;
import com.Ruier.pojo.PageBean;
import com.Ruier.pojo.Result;
import com.Ruier.pojo.query.HeroQuery;
import com.Ruier.service.HeroService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/heroes")
public class HeroController {
    @Autowired
    private HeroService heroService;
    @PostMapping("/addhero")
    public Result addHero(@RequestBody Hero newHero){
        boolean addHeroSuccess = heroService.addHero(newHero);
        if(addHeroSuccess){
            return Result.success();
        }else return Result.error("此英雄已添加！");
    }
    @GetMapping("/selectbypage")
    public Result selectByPage(int currentPage, int pageSize){
        PageBean<Hero> pageBean = heroService.selectByPage(currentPage, pageSize);
        return Result.success(pageBean);
    }
    @GetMapping("/reshow")
    public Result reShowHero(Long heroId){
        Hero hero = heroService.reshowHero(heroId);
        return Result.success(hero);
    }
    @PutMapping("/updatehero")
    public Result updateHero(@RequestBody Hero updateHero){
        heroService.updateHero(updateHero);
        return Result.success();
    }
    @DeleteMapping("/deletehero/{heroId}")
    public Result deleteHero(@PathVariable Long heroId){
        heroService.deleteHero(heroId);
        return Result.success();
    }
    @DeleteMapping("/deleteherobyids")
    public Result deleteByIds(@RequestBody List<HeroQuery> selectedHero){
        heroService.deleteHeroByIds(selectedHero);
        return Result.success();
    }
    @PostMapping("/searchhero")
    public Result searchHero(@RequestBody Hero ConditionHero){
        PageBean<HeroQuery> pageBean = heroService.searchHeroes(ConditionHero);
        return Result.success(pageBean);
    }
}
